package com.sctvcloud.dongqu.ui.utils;

import android.graphics.drawable.Drawable;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.jiongbull.jlog.JLog;

/**
 * Created by liuyu on 2017/10/11.
 */

public class ItemImagTarget extends GlideDrawableImageViewTarget {

    private float mxW = -1F;

    private String tag;
    private int height = 0;
    private int width = 0;

    public ItemImagTarget(ImageView view) {
        super(view);
    }

    public ItemImagTarget(ImageView view, int height, int width) {
        super(view);
        this.height = height;
        this.width = width;
    }

    @Override
    public void onStart() {
        if (view.getWidth() == 0 || view.getHeight() == 0) {
            super.onStart();
        }
    }

    @Override
    public void onLoadFailed(Exception e, Drawable errorDrawable) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        boolean b = false;
        if (lp.width > 0) {
            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            b = true;
        }
        if (lp.height > 0) {
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            b = true;
        }
        if (b) {
            view.setLayoutParams(lp);
        }
        super.onLoadFailed(e, errorDrawable);
    }

    @Override
    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
        if (width != 0 && height != 0) {
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            if (width > height){

            }
        } else {
            int vw = view.getWidth();
            float vh = view.getHeight();
            float riw = resource.getIntrinsicWidth();
            float rih = resource.getIntrinsicHeight();
//        JLog.e("ajjj  riw=" + riw + "  rih=" + rih + "  rmw=" + resource.getMinimumWidth()+"   "+tag
//                + "  rmh=" + resource.getMinimumHeight() + "\nvw=" + vw + "  vh=" + vh + "  " + view.getMaxHeight()
//                + "   " + view.getMaxWidth() + "   " + this);
            int q = 0;
            if (vw > 0 && (Math.abs(vw - riw) > 10 || Math.abs(vh - rih) > 10)) {
                ViewGroup.LayoutParams lp = view.getLayoutParams();
                q = 101;
                float fx = 0;
                if (vh >= rih) {
                    setScaleType(ImageView.ScaleType.FIT_XY);
                    q = 111;
                    fx = vw - riw;
                    lp.height = (int) rih;
                    if (fx > 10) {
                        q = 112;
                        lp.width = (int) riw;
                    } else if (fx < -10) {
                        q = 1131;
                        if (mxW > 0 && riw <= mxW) {
                            q = 1132;
                            lp.width = (int) riw;
                        } else if (lp.width > 0) {
                            q = 1133;
                            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                        } else {
                            q = 1134;
                            lp = null;
                        }
                    } else {
                        q = 114;
                        lp = null;
                    }
                } else {
                    q = 121;
                    fx = rih - view.getMaxHeight();
                    if (fx > 10) {
                        q = 1211;
                        lp.height = view.getMaxHeight();
                        if (rih / riw >= 5) {
                            setScaleType(ImageView.ScaleType.CENTER_CROP);
                            lp.width = (int) (0.75 * view.getMaxHeight());
                        } else {
                            setScaleType(ImageView.ScaleType.FIT_XY);
                            int w = (int) (riw / rih * view.getMaxHeight());
                            if (mxW > 0 && w <= mxW) {
                                q = 1213;
                                lp.width = w;
                            } else if (lp.width > 0) {
                                q = 1214;
                                lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                            }
                        }
                    } else if (fx < -10) {
                        setScaleType(ImageView.ScaleType.FIT_XY);
                        lp.height = (int) rih;
                        q = 1221;
                        if (mxW > 0 && riw <= mxW) {
                            q = 1222;
                            lp.width = (int) riw;
                        } else if (lp.width > 0) {
                            q = 1223;
                            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                        }
                    } else {
                        q = 131;
                        lp = null;
                    }
                }
                if (lp != null) {
                    view.setLayoutParams(lp);
                }
//            JLog.e("ajjj  lp=" + lp + "   q=" + q + "   mxw=" + mxW+"   "+tag);
            }
            super.onResourceReady(resource, animation);
            if (view != null) {
//            JLog.e("ajjj will requestLayout  ");
//            view.requestLayout();
            }
        }
    }

    private void setScaleType(ImageView.ScaleType type) {
        if (view == null || type == null) {
            return;
        }
        if (view.getScaleType() != type) {
            view.setScaleType(type);
        }
    }

    public void setMxW(float mxW) {
        if (mxW <= 0) {
            this.mxW = -1;
        } else {
            this.mxW = mxW;
        }
    }

    public void setTag(String tag) {
        this.tag = tag;
    }
}
